﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using OpenExpressApp.Module.WPF;
using OpenExpressApp.Editors;

namespace OpenExpressApp.Module.WPF.Editors
{
    public class GDataGridBooleanColumn : OpenDataGridColumn
    {
        private BooleanPropertyEditor _editor;

        public override IWPFPropertyEditor Editor
        {
            get
            {
                return this._editor;
            }
        }

        public GDataGridBooleanColumn(BusinessObjectPropertyInfo info, IListObjectView view)
            : base(info, view)
        {
            _editor = new BooleanPropertyEditor(this.PropertyInfo, this.View);
        }

        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            CheckBox cb = new CheckBox();
            Binding TextBinding = new Binding(PropertyInfo.Name);
            cb.SetBinding(CheckBox.IsCheckedProperty, TextBinding);
            cb.HorizontalAlignment = HorizontalAlignment.Center;
            cb.VerticalAlignment = VerticalAlignment.Center;
            var oldValue = cb.IsChecked;
            cb.Checked += (o, e) =>
                {
                    if (this._editor.IsReadOnly)
                    {
                        cb.IsChecked = oldValue;
                    }
                };

            return cb;
        }

        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            var editor = this.Editor;
            editor.EnsureControlsCreated();
            editor.Control.PreviewMouseLeftButtonDown += (o,e) =>
                {
                    if (this._editor.IsReadOnly)
                    {
                        e.Handled = true;
                    }
                };
            return editor.Control;
        }
    }
}
